﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 学生管理系统
{
    public partial class Form3 : Form
    {
        public DataSet ds;
        public SqlConnection conn;
        public DataTable dt = new DataTable();
        public SqlCommand command;
        public SqlCommandBuilder builder;
        public SqlDataAdapter sda;

        public Form3()
        {
            InitializeComponent();
            string connstring = @"Data Source=SWH;Initial Catalog=bigtask;Integrated Security=True";
            conn = new SqlConnection(connstring);
            conn.Open();
            string sql = "select classID,className from Class";
            command = new SqlCommand(sql, conn);
            sda = new SqlDataAdapter(sql, conn);
            sda.SelectCommand = command;
            builder = new SqlCommandBuilder(sda);
            ds = new DataSet();
            sda.TableMappings.Add("Class", "班级信息表");
            sda.TableMappings[0].ColumnMappings.Add("classID", "课程号");
            sda.TableMappings[0].ColumnMappings.Add("className", "班级名");
            
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            try
            {
                sda.Fill(ds, "Class");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                conn.Close();
            }
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void Add_btn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定添加？", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                try
                {
                    sda.Update(ds.Tables[0].GetChanges());
                    MessageBox.Show("添加成功", "成功信息");
                    ds.Tables[0].AcceptChanges();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        private void Del_btn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除当前行数据？", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                try
                {
                    ds.Tables[0].Rows[dataGridView1.CurrentRow.Index].Delete();
                    sda.Update(ds.Tables[0].GetChanges());
                    MessageBox.Show("数据删除成功！");
                    ds.Tables[0].AcceptChanges();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                ds.Tables[0].RejectChanges();
            }
        }

        private void Alter_btn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定修改当前行数据？", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                    try
                {
                    sda.Update(ds.Tables[0].GetChanges());
                    MessageBox.Show("数据修改成功", "成功信息");
                    ds.Tables[0].AcceptChanges();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }
}
